package com.idp.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.idp.mapper.UsersMapper;
import com.idp.pojo.MyUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author JTR -H
 * @Address BS_221
 * @Created_Date 2021/7/28 - 19:03
 */
@Service
public class UsersService {

    private static final Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);

    @Autowired
    private UsersMapper usersMapper;

    public int insertUsers(MyUser user) throws UsernameNotFoundException {
        int insert;
        try {
            insert = usersMapper.insert(user);
        } catch (DuplicateKeyException e) {
            insert = -1;    // username重复
            e.printStackTrace();
        }
        logger.info("insertUsers: " + insert);
        return insert;
    }

//    public void updatePasswordByUsername(String username, String password) {
//        UpdateWrapper<MyUser> wrapper = new UpdateWrapper<>();
//        wrapper.eq("username", username).set("password", password);
//        int update = usersMapper.update(null, wrapper);
//        logger.info("updatePasswordByUsername: " + update);
//    }

//    public void updateAuthorityByUsername(String username, String authority) {
//        UpdateWrapper<MyUser> wrapper = new UpdateWrapper<>();
//        wrapper.eq("username", username).set("authority", authority);
//        int update = usersMapper.update(null, wrapper);
//        logger.info("updateAuthorityByUsername: " + update + " authority: " + authority);
//    }

}


/*
 ******************************************************
 *    Eliminate all impossible,                       *
 *    remain that although or else can be thought of, *
 *    discuss,                                        *
 *    that also is a fact.                            *
 ******************************************************
 */
